package com.to8to.scene.data;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 本类用于构造动态代理对象
 * 
 * @author Ervin.zhang
 *
 */
public class DataFixProxyFactory
{

    /**
     * 构造动态代理对象
     * 
     * @param target
     *            被代理对象
     * @param advice
     *            方法通知
     * @return 代理对象
     */
    public static Object getProxy(Object target, DataFixAdvice advice)
    {
        return Proxy.newProxyInstance(DataFixProxyFactory.class.getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler()
        {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
            {
                advice.before(target.getClass(), method);
                long startTime = System.currentTimeMillis();

                method.invoke(target, args);

                long endTime = System.currentTimeMillis();
                advice.after(target.getClass(), method, endTime - startTime);
                return null;
            }
        });
    }
}
